<?php

namespace app\admin\controller\miniform;

use app\common\controller\Backend;
use fast\Random;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 项目
 *
 * @icon fa fa-circle-o
 */
class Project extends Backend
{

    /**
     * Project模型对象
     * @var \app\admin\model\miniform\Project
     */
    protected $model = null;
    protected $multiFields = "is_multi,is_signin,is_verification,iscaptcha,front_back,status";
    protected $noNeedRight = ['get_project_list'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\miniform\Project;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("front_back_list", $this->model->getFrontBackList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $fields = \app\admin\model\miniform\Fields::where('source_id', $ids)->where('source', 'project')->order("weigh DESC, id ASC")->select();
        foreach ($fields as $index => &$field) {
            if ($field['type'] === 'location') {
                $json = (array)json_decode($field['value'], true);
                if (!isset($json['address'])) {
                    $address = '';
                    $lat = 0;
                    $lng = 0;
                    if (preg_match("/(.*)\,([0-9\.]+),([0-9\.]+)\$/i", $field['value'], $matches)) {
                        $address = $matches[1];
                        $lat = $matches[2];
                        $lng = $matches[3];
                    }
                    $field['value'] = ['address' => $address, 'lat' => $lat, 'lng' => $lng];
                } else {
                    $field['value'] = $json;
                }
            }
        }
        $this->assign('fields', $fields);
        return parent::edit($ids);
    }

    /**
     * 还原
     */
    public function restore($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        if ($ids) {
            $this->model->where($pk, 'in', $ids);
        }
        $count = 0;
        Db::startTrans();
        try {
            $list = $this->model->onlyTrashed()->select();
            foreach ($list as $index => $item) {
                $count += $item->restore();
                //还原日志
                \addons\miniform\model\Logs::restoreLogs($item['id']);
            }
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success();
        }
        $this->error(__('No rows were updated'));
    }

    /**
     * 真实删除
     */
    public function destroy($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        if ($ids) {
            $this->model->where($pk, 'in', $ids);
        }
        $count = 0;
        Db::startTrans();
        try {
            $list = $this->model->onlyTrashed()->select();
            foreach ($list as $k => $v) {
                $count += $v->delete(true);
                //删除项目字段-表单字段
                \app\admin\model\miniform\Fields::deleteField(function ($query) use ($v) {
                    $query->where('source_id', $v['id']);
                }, true);
                //删除项目日志
                \addons\miniform\model\Logs::deleteLogs($v['id'], true);
                //删除表单
                \app\admin\model\miniform\Project::deleteForm($v['table']);
            }
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success();
        } else {
            $this->error(__('No rows were deleted'));
        }
    }

    /**
     * 获取项目列表
     * @return mixed
     */
    public function get_project_list()
    {
        $list = $this->selectpage();
        $result = $list->getData();
        $primaryvalue = $this->request->request("keyValue");
        if ($primaryvalue) {
            if (in_array('*', explode(',', $primaryvalue))) {
                array_unshift($result['list'], ['id' => '*', 'title' => '全部项目']);
            }
        } else {
            array_unshift($result['list'], ['id' => '*', 'title' => '全部项目']);
        }
        return $result;
    }

    /**
     * 复制选择行
     * @param string $ids
     */
    public function copy($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $projectList = $this->model->where('id', 'in', $ids)->select();
            foreach ($projectList as $index => $item) {
                try {
                    $data = Db::name('miniform_project')->find($item['id']);
                    $fieldsList = Db::name('miniform_fields')->where('source_id', $data['id'])->field('id', true)->select();

                    $data = array_merge($data, $addon ?? []);
                    $exist = \app\admin\model\miniform\Project::where('table', $item['table'])->find();
                    $copyTag = $exist ? "_" . strtolower(Random::alpha(6)) : "_copy";

                    $data['table'] = $data['table'] . $copyTag;
                    $data['title'] = $data['title'] . $copyTag;
                    $data['views'] = $data['registered'] = 0;
                    $data['createtime'] = $data['updatetime'] = time();
                    $data['status'] = 'hidden';
                    unset($data['id']);
                    if (strlen($data['table']) > 100) {
                        throw new Exception("字段表名过长，无法完成复制");
                    }
                    $project_id = \think\Db::name('miniform_project')->insert($data, false, true);
                    foreach ($fieldsList as $subindex => &$subitem) {
                        $subitem['source'] = $subitem['source'] == 'project' ? 'project' : $data['table'];
                        $subitem['source_id'] = $project_id;
                    }
                    unset($subitem);
                    (new \app\admin\model\miniform\Fields())->insertAll($fieldsList);

                    //复制表结构
                    $prefix = Config::get('database.prefix');
                    Db::execute("CREATE TABLE `{$prefix}miniform_{$data['table']}` LIKE `{$prefix}miniform_{$item['table']}`");

                } catch (\Exception $e) {
                    $this->error("发生错误：" . $e->getMessage());
                }
            }
            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
